const { ccclass, property } = cc._decorator;

@ccclass
class LevelEditorDataMgr {
    private static instance: LevelEditorDataMgr;

    public initGameInfo: {
        level: null | number;
        totalStep: null | number;
        mapArr: null | any[];
        middleSquareData: null | any;
        bottomSquareData: null | any;
        topSquareData: null | any;
        fixedDropData: any[];
        probabilityDropData: any[];
        targetMatchData: any[];
        levelCategory: null | any;
        haveNewObstce: null | any;
        guideIndex: number;
    };
    private choseItem: any;

    constructor() {
        this.initGameInfo = {
            level: null,
            totalStep: null,
            mapArr: null,
            middleSquareData: null,
            bottomSquareData: null,
            topSquareData: null,
            fixedDropData: [],
            probabilityDropData: [],
            targetMatchData: [],
            levelCategory: null,
            haveNewObstce: null,
            guideIndex: 0
        };
        this.choseItem = null;
    }

    public static getInstance(): LevelEditorDataMgr {
        if (!LevelEditorDataMgr.instance) {
            LevelEditorDataMgr.instance = new LevelEditorDataMgr();
        }
        return LevelEditorDataMgr.instance;
    }

    public initMapLayOutData(e: any): void {
        if (e) {
            var t = this.cloneMaplayOutArr(e);
            this.initGameInfo.mapArr = t;
        } else {
            console.log("初始化地图数据不存在==========");
        }
    }

    public initBottomLayerData(e: any): void {
        if (e) {
            var t = this.cloneMaplayOutArr(e);
            this.initGameInfo.bottomSquareData = t;
        } else {
            console.log("初始化BottomLaye数据不存在==========");
        }
    }

    public initMiddleLayerData(e: any): void {
        if (e) {
            var t = this.cloneMaplayOutArr(e);
            this.initGameInfo.middleSquareData = t;
        } else {
            console.log("初始化地图数据不存在==========");
        }
    }

    public initTopLayerData(e: any): void {
        if (e) {
            var t = this.cloneMaplayOutArr(e);
            this.initGameInfo.topSquareData = t;
        } else {
            console.log("初始化地图数据不存在==========");
        }
    }

    public updateMapData(e: number, t: number, a: any): void {
        if (this.initGameInfo.mapArr[e][t] !== null) {
            this.initGameInfo.mapArr[e][t] = a;
        }
        console.log("更新底部数据==========", this.initGameInfo.mapArr);
    }

    public updateBottomData(e: number, t: number, a: any): void {
        if (this.initGameInfo.bottomSquareData[e][t] !== null) {
            this.initGameInfo.bottomSquareData[e][t] = a;
        }
        console.log("更新low数据数据==========", this.initGameInfo.bottomSquareData);
    }

    public updateMiddleData(e: number, t: number, a: any): void {
        if (this.initGameInfo.middleSquareData[e][t] !== null) {
            this.initGameInfo.middleSquareData[e][t] = a;
        }
        console.log("更新middle数据数据==========", this.initGameInfo.middleSquareData, e, t, a);
    }

    public updateTopData(e: number, t: number, a: any): void {
        if (this.initGameInfo.topSquareData[e][t] !== null) {
            this.initGameInfo.topSquareData[e][t] = a;
        }
        console.log("更新top数据数据==========", this.initGameInfo.topSquareData);
    }

    private cloneMaplayOutArr(e: any) {
        let t: any[][] = [[]];
        for (let a = 0; a < e.length; a++) {
            t[a] = [];
            for (let o = e[a], i = 0; i < o.length; i++) {
                t[a][i] = o[i];
            }
        }
        return t;
    }

    public setChoseItem(e: any): void {
        this.choseItem = e;
    }

    public getChoseItem(): any {
        return this.choseItem;
    }

    public checkBottomCanPlace(e: any): boolean {
        const t = this.initGameInfo.mapArr;
        for (let a = 0; a < e.length; a++) {
            const o = e[a];
            const i = t[o.row][o.col];
            if (i === 2 || i === 3) {
                return false;
            }
        }
        return true;
    }

    public checkMiddleCanPlace(e: any): boolean {
        const t = this.initGameInfo.middleSquareData;
        for (let a = 0; a < e.length; a++) {
            const o = e[a];
            if (t[o.row][o.col] !== "N") {
                return false;
            }
        }
        return true;
    }

    public checkTopCanPlace(e: any): boolean {
        const t = this.initGameInfo.topSquareData;
        for (let a = 0; a < e.length; a++) {
            const o = e[a];
            if (t[o.row][o.col] !== "N") {
                return false;
            }
        }
        return true;
    }

    public setRelationData(e: any, t: any, a: boolean = false): void {
        for (let o = 0; o < e.length; o++) {
            const i = e[o];
            if (!(i.row === t.row && i.col === t.col)) {
                if (a) {
                    this.updateTopData(i.row, i.col, "E");
                } else {
                    this.updateMiddleData(i.row, i.col, "E");
                }
            }
        }
    }

    public clearRelationData(e: any, t: boolean = false): void {
        for (let a = 0; a < e.length; a++) {
            const o = e[a];
            if (t) {
                this.updateTopData(o.row, o.col, "N");
            } else {
                this.updateMiddleData(o.row, o.col, "N");
            }
        }
    }

    public getPerLayerCanPlace(e: number, t: number): number[] {
        const a = this.initGameInfo;
        const o = [a.mapArr, a.bottomSquareData, a.middleSquareData, a.topSquareData];
        const i = [];
        for (let r = 0; r < o.length; r++) {
            const n = o[r];
            let l = 0;
            if (n[e] === null || (n[e] && n[e][t] === null)) {
                l = -1;
                i.push(l);
            } else {
                if (r === 0) {
                    l = (n[e][t] === 2 || n[e][t] === 3) ? 0 : 1;
                    i.push(l);
                } else {
                    l = (n[e][t] === "N") ? 1 : 0;
                    i.push(l);
                }
            }
        }
        return i;
    }

    public setFixedDropData(e: any): void {
        this.initGameInfo.fixedDropData = e;
        console.log("设置固定刷新数据========", this.initGameInfo.fixedDropData);
    }

    public setProbabilityDropData(e: any): void {
        this.initGameInfo.probabilityDropData = e;
        console.log("设置兜底刷新数据========", this.initGameInfo.probabilityDropData);
    }

    public isMiddleHaveHole(): boolean {
        const e = this.initGameInfo.middleSquareData;
        const t = this.initGameInfo.mapArr;
        for (let a = 0; a < e.length; a++) {
            const o = e[a];
            for (let i = 0; i < o.length; i++) {
                const r = t[a][i];
                const n = o[i];
                if (r === 1 && n === "N") {
                    return true;
                }
            }
        }
        return false;
    }

    public resetGameInfoData(): void {
        this.initGameInfo = {
            level: null,
            totalStep: null,
            mapArr: null,
            middleSquareData: null,
            bottomSquareData: null,
            topSquareData: null,
            fixedDropData: [],
            probabilityDropData: [],
            targetMatchData: [],
            levelCategory: null,
            haveNewObstce: null,
            guideIndex: 0
        };
    }

    public updateGameInfoData(e: any): void {
        if (e) {
            this.initGameInfo = {
                level: e.level,
                totalStep: e.totalStep,
                mapArr: e.mapArr,
                middleSquareData: e.middleSquareData,
                bottomSquareData: e.bottomSquareData,
                topSquareData: e.topSquareData,
                fixedDropData: e.fixedDropData,
                probabilityDropData: e.probabilityDropData,
                targetMatchData: e.targetMatchData,
                levelCategory: e.levelCategory,
                haveNewObstce: e.haveNewObstce,
                guideIndex: e.guideIndex || 0
            };
        } else {
            console.log("更新的数据不存在=======请检查");
        }
    }

    public isNewProbabilityDrop(): boolean {
        const e = this.initGameInfo.probabilityDropData;
        for (let t = 0; t < e.length; t++) {
            if (e[t].hasOwnProperty("squareTypeStr")) {
                return true;
            }
        }
        return false;
    }

    public isCanPlaceCupHolder(e: any): boolean {
        const t = this.initGameInfo.middleSquareData;
        for (let a = 0; a < e.length; a++) {
            const o = e[a].row;
            const i = e[a].col;
            const r = t[o][i];
            if (t[o] && t[o][i] && r !== "N") {
                return false;
            }
        }
        return true;
    }
}

export default LevelEditorDataMgr.getInstance();